package de.fmannan.wildit.ui;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import de.fmannan.wildit.persistence.PersistenceProvider;
import de.fmannan.wildit.service.AlarmSetter;

public class DayScreen extends Activity{

	private static final String TAG = "DayScreen";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.d(getClass().toString(), "Creating Day Screen");
		
		//Set layout.xml as layout
		setContentView(R.layout.dayscreenlayout);
		
		addListeners();
	}
	
	private void addListeners()
	{
		//Add listeners to the reality check settings button which leads to a submenu
		TextView realityCheckSettings = (TextView)findViewById(R.id.realityCheckSettings);
		realityCheckSettings.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), RealityCheckSettings.class);
				Log.d(TAG, "Launching Sub activity - reality check settings list view");
				v.getContext().startActivity(i);
			}
			
		});		
		//Add listeners to the reality check settings checkbox - if it's check we try to set alarms accordingly
		//and give a message to the user (Toas style)
		CheckBox enableRealityCheckAlarm = (CheckBox)findViewById(R.id.realityCheckAlarmEnabled);
		enableRealityCheckAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked)
				{
					//Try to set the alarm
					Calendar start = PersistenceProvider.getPersistenceProvider(buttonView.getContext()).getAlarmStartTime();
					Calendar end = PersistenceProvider.getPersistenceProvider(buttonView.getContext()).getAlarmEndTime();
					Calendar interval = PersistenceProvider.getPersistenceProvider(buttonView.getContext()).getAlarmInterval();
					//Make sure all our values are valid calendar objects, i.e. not null
					if(start != null && end != null && interval != null)
					{
						AlarmSetter.scheduleAlarm(buttonView.getContext(), start, end, interval);
					}			
				}
				else
				{
					//Stop the alarm
				}
			}
			
		});
	}
}
